package com.wintop.springbootjwt.config;

import com.wintop.springbootjwt.interceptor.UserSecurityHandlerInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class DefaultWebMvcConfigurer implements WebMvcConfigurer {

    @Autowired
    UserSecurityHandlerInterceptor userSecurityHandlerInterceptor;

    /**
     * 配置跨域请求
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //定义哪些URL接受跨域
        registry.addMapping("/**")
                //定义哪些origin可以跨域请求
                .allowedOrigins("*")
                //定义接受的跨域请求方法
                .allowedMethods("POST", "GET", "PUT", "PATCH", "OPTIONS", "DELETE")
                .exposedHeaders("Set-Token")
                .allowCredentials(true)
                .allowedHeaders("*")
                .maxAge(3600);
    }

    /**
     * 把自定义拦截器器注册到Spring容器中 并排除路径
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userSecurityHandlerInterceptor).addPathPatterns("/**").excludePathPatterns("/login", "/dev/**");
    }



}
